package com.webchat.sso.controller;

import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.service.MinioService;
import com.webchat.common.util.DateUtils;
import com.webchat.domain.vo.response.UploadResultVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@RestController
@RequestMapping("/sso-service/file")
public class FileController {

    @Autowired
    private MinioService minioService;
    @Autowired
    private HttpServletRequest request;

    @PostMapping("/upload")
    public APIResponseBean<UploadResultVO> upload(MultipartFile file) {
        String path = request.getHeader("upload-path");
        Assert.isTrue(StringUtils.isNotBlank(path), "上传路径不能为空");
        String timePackage = DateUtils.getDate2String(DateUtils.YYYYMMDD, new Date());
        UploadResultVO uploadResultDTO = minioService.upload(path + "/" + timePackage, file, true);
        return APIResponseBeanUtil.success(uploadResultDTO);
    }

    @GetMapping("/download")
    public void download(@RequestParam String filename, HttpServletResponse response) {
        minioService.download(filename, response);
    }
}
